package com.errol.generic.file.excel.service;

import com.renjia.service.generic.file.core.pojo.Filepath;
import com.renjia.service.generic.file.excel.model.ExcelDynamicColumn;
import com.renjia.service.generic.file.excel.model.ExcelResolved;
import com.renjia.service.generic.file.excel.pojo.ExcelValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Errol1017 on 2018/6/13.
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2020/10/21
 * @since v4.7.6
 */
@Service
class ExcelServiceImpl implements ExcelService {

    private final ExcelInnerReaderServiceImpl excelInnerReaderService;
    private final ExcelInnerWriterServiceImpl excelInnerWriterService;

    @Autowired
    public ExcelServiceImpl(ExcelInnerReaderServiceImpl excelInnerReaderService, ExcelInnerWriterServiceImpl excelInnerWriterService) {
        this.excelInnerReaderService = excelInnerReaderService;
        this.excelInnerWriterService = excelInnerWriterService;
    }

    @Override
    public <R> ExcelResolved<R> readFromExcel(@NonNull String from, @NonNull Class<R> clazz) throws Exception {
        return excelInnerReaderService.readFromExcel(from, clazz, new HashMap<>(), null);
    }

    @Override
    public <R> ExcelResolved<R> readFromExcel(@NonNull String from, @NonNull Class<R> clazz, @NonNull Map<String, Map<String, String>> dynamicSources) throws Exception {
        return excelInnerReaderService.readFromExcel(from, clazz, dynamicSources, null);
    }

    @Override
    public <R> ExcelResolved<R> readFromExcel(@NonNull String from, @NonNull Class<R> clazz, @NonNull ExcelValidator<R> excelValidator) throws Exception {
        return excelInnerReaderService.readFromExcel(from, clazz, new HashMap<>(), excelValidator);
    }

    @Override
    public <R> ExcelResolved<R> readFromExcel(@NonNull String from, @NonNull Class<R> clazz, @NonNull Map<String, Map<String, String>> dynamicSources, @NonNull ExcelValidator<R> excelValidator) throws Exception {
        return excelInnerReaderService.readFromExcel(from, clazz, dynamicSources, excelValidator);
    }

    @Override
    public <R> String writeToExcel(@NonNull List<R> data, @NonNull Filepath toDir, @NonNull Class<R> clazz) throws Exception {
        return excelInnerWriterService.writeToExcel(data, toDir, clazz);
    }

    @Override
    public String writeToExcel(@NonNull List<ExcelDynamicColumn> header, @NonNull List<List<Object>> data, @NonNull Filepath toDir) throws Exception {
        return excelInnerWriterService.writeToExcel(header, data, toDir, "");
    }

    @Override
    public String writeToExcel(@NonNull List<ExcelDynamicColumn> header, @NonNull List<List<Object>> data, @NonNull Filepath toDir, @NonNull String title) throws Exception {
        return excelInnerWriterService.writeToExcel(header, data, toDir, title);
    }


}
